package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.GoodsList;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 首页搜索跳转商品列表界面数据适配器
 * <p/>
 *
 */
public class GoodsListAdapter extends BaseAdapter {

    private Context context;
    private List<GoodsList> list;


    public GoodsListAdapter(Context context, List<GoodsList> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            //判断缓存中是否有内容  没有则创建 有的话 使用缓存中的内容
            holder = new ViewHolder();
            //绑定布局文件
            view = LayoutInflater.from(context).inflate(R.layout.goodslist_item_list, null);
            //绑定控件id
            holder.goods_image = (ImageView) view.findViewById(R.id.goodsList_image);
            holder.goods_name = (TextView) view.findViewById(R.id.goods_name);
            holder.goods_price = (TextView) view.findViewById(R.id.goods_price);
            //holder.evaluation_good_star = (TextView) view.findViewById(R.id.evaluation_good_star);
            holder.evaluation_count = (TextView) view.findViewById(R.id.evaluation_count);
            //将内容放到缓存中
            view.setTag(holder);
        }else {
            //使用缓存中的内容
            holder = (ViewHolder) view.getTag();

        }

        GoodsList goodsList = list.get(i);
        //使用Picasso 加载图片
        Picasso.with(context).load(goodsList.getGoods_image_url()).into(holder.goods_image);
        holder.goods_name.setText(goodsList.getGoods_name());
        holder.goods_price.setText(goodsList.getGoods_price());
        //holder.evaluation_good_star.setText(goodsList.getEvaluation_good_star());
        holder.evaluation_count.setText(goodsList.getEvaluation_count());

        return view;
    }


    class ViewHolder {

        private ImageView goods_image;
        private TextView goods_name;
        private TextView goods_price;
        //private TextView evaluation_good_star;
        private TextView evaluation_count;


    }
}
